<script lang="ts" setup>
import { computed, reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { storeToRefs } from "pinia"
import { useAppStore } from "@/store/modules/app"
import { useSettingsStore } from "@/store/modules/settings"
import { useUserStore } from "@/store/modules/user"
import { UserFilled } from "@element-plus/icons-vue"
import Hamburger from "../Hamburger/index.vue"
import Breadcrumb from "../Breadcrumb/index.vue"
import Sidebar from "../Sidebar/index.vue"
import { DeviceEnum } from "@/constants/app-key"

const router = useRouter()
const appStore = useAppStore()
const settingsStore = useSettingsStore()
const userStore = useUserStore()

const { sidebar, device } = storeToRefs(appStore)
const { layoutMode } = storeToRefs(settingsStore)

const isTop = computed(() => layoutMode.value === "top")
const isMobile = computed(() => device.value === DeviceEnum.Mobile)

/** 切换侧边栏 */
const toggleSidebar = () => {
  appStore.toggleSidebar(false)
}

/** 登出 */
const logout = () => {
  userStore.logout()
  router.push("/login")
}
const user = ref()
const dialogVisible = ref<boolean>(false)
const updateUser = () => {
  user.value = userStore.getLocalUserInfo()
  dialogVisible.value = true
}
</script>

<template>
  <div class="navigation-bar">
    <Hamburger v-if="!isTop || isMobile" :is-active="sidebar.opened" class="hamburger" @toggle-click="toggleSidebar" />
    <Breadcrumb v-if="!isTop || isMobile" class="breadcrumb" />
    <Sidebar v-if="isTop && !isMobile" class="sidebar" />
    <div class="right-menu">
      <el-dropdown class="right-menu-item">
        <div class="right-menu-avatar">
          <el-avatar :icon="UserFilled" :size="30" />
          <span>{{ userStore.getLocalUserInfo().userAccount }}</span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <a target="_blank" @click="updateUser"> <el-dropdown-item>个人信息</el-dropdown-item> </a>
            <el-dropdown-item divided @click="logout">
              <span style="display: block">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <!-- 新增/修改 -->
    <el-dialog v-model="dialogVisible" width="30%">
      <el-form ref="formRef" :model="user" label-width="100px" label-position="left">
        <el-form-item prop="userName" label="姓名" v-if="user.userName !== null">
          <el-input v-model="user.userName" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="joinSchool" label="学校">
          <el-input v-model="user.joinSchool" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="userRole" label="权限">
          <el-input v-model="user.userRole" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="reviewRange" label="评选范围" v-if="user.reviewRange === ' ' || user.userRole === 'judge'">
          <el-select v-model="user.reviewRange" placeholder="请选择" style="width: 240px">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item prop="userAccount" label="账号">
          <el-input v-model="user.userAccount" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="userPassword" label="密码">
          <el-input v-model="user.userPassword" placeholder="请输入" type="password" show-password />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
.navigation-bar {
  height: var(--v3-navigationbar-height);
  overflow: hidden;
  background: var(--v3-header-bg-color);
  display: flex;
  justify-content: space-between;
  .hamburger {
    display: flex;
    align-items: center;
    height: 100%;
    padding: 0 15px;
    cursor: pointer;
  }
  .breadcrumb {
    flex: 1;
    // 参考 Bootstrap 的响应式设计将宽度设置为 576
    @media screen and (max-width: 576px) {
      display: none;
    }
  }
  .sidebar {
    flex: 1;
    // 设置 min-width 是为了让 Sidebar 里的 el-menu 宽度自适应
    min-width: 0px;
    :deep(.el-menu) {
      background-color: transparent;
    }
    :deep(.el-sub-menu) {
      &.is-active {
        .el-sub-menu__title {
          color: var(--el-menu-active-color) !important;
        }
      }
    }
  }
  .right-menu {
    margin-right: 10px;
    height: 100%;
    display: flex;
    align-items: center;
    color: #606266;
    .right-menu-item {
      padding: 0 10px;
      cursor: pointer;
      .right-menu-avatar {
        display: flex;
        align-items: center;
        .el-avatar {
          margin-right: 10px;
        }
        span {
          font-size: 16px;
        }
      }
    }
  }
}
</style>
